{load href="/public/editor/ueditor.config.js"}
{load href="/public/editor/ueditor.all.js"}

<section class="page-hd">
    <header>
        <p class="title-description title-top">
            添加Banner图片
        </p>
    </header>
    <hr>
</section>
<form method="post" id="userForm" enctype="multipart/form-data">
    <input type="hidden" name="id" value="{$model.id}">

    <div class="form-group-col-2">
        <div class="form-label">类型：</div>
        <div class="col-sm-5">
            <select style="width:auto;" name="typeId">
                <option value="">请选择类型</option>
                {volist name="list" id="vo"}
                {if condition="$vo.id == $model.typeId"}
                <option value="{$vo.id}" selected>{$vo.typeName}</option>
                {else/}
                <option value="{$vo.id}">{$vo.typeName}</option>
                {/if}
                {/volist}
            </select>
        </div>
    </div>

    <div class="form-group-col-2">
        <div class="form-label">名称：</div>
        <div class="col-sm-5">
            <input type="text" name="name" placeholder="名称..." class="form-control form-boxed form-width"
                   value="{$model.name}">
        </div>
    </div>

    <div class="form-group-col-2">
        <div class="form-label">排序：</div>
        <div class="col-sm-5">
            {assign name="sort" value="255"}
            {if condition="!empty($model.sort)"}
            {assign name="sort" value="$model.sort"}
            {/if}
            <input type="text" name="sort" placeholder="排序..." class="form-control form-boxed form-width"
                   value="{$sort}">
        </div>
    </div>

    <div class="form-group-col-2">
        <div class="form-label">上传图片：</div>
        <div class="col-sm-5">
            <input type="file" class="form-control" name="img"><br>
            {if condition='!empty($model.img)'}
            <img src="__UPLOAD__{$model.img}" style="height: 50px;">
            {/if}
        </div>
    </div>

    <div class="form-group-col-2">
        <div class="form-label"></div>
        <div class="col-sm-5">
            <input type="button" id="userSubmit" class="btn btn-primary" value="添加图片"
                   data-url="{:url('banner/addAndUpdateNews')}"/>
        </div>
    </div>
</form>

<script>
    $('#userSubmit').on('click', function () {
        var url = $(this).attr('data-url');
        Ajax(url, new FormData($('#userForm')[0]));
    });

    function Ajax(url, data) {
        $.ajax({
            url: url,
            cache: false,
            dataType: 'json',
            data: data,
            type: 'post',
            processData: false,
            contentType: false,
        }).done(function (res) {
            if (res.code == 200) {
                layer.alert(res.msg, function () {
                    location.href = '{:url("banner/banner_list")}';
                });
            } else {
                layer.alert(res.msg);
            }
        }).fail(function (e) {
            layer.alert(e);
        });
    }
</script>